package ModelLayer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.io.FileNotFoundException;

/**
 * Write a description of class FileDealer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FileCustomer
{
    // instance variables - replace the example below with your own
    private File file;
    private BufferedWriter bw;
    private FileWriter fw;
    private BufferedReader br;
    private FileReader fr;
    private ArrayList<Customer> old_customers;
    

    /**
     * Constructor for objects of class FileDealer
     */
    public FileCustomer()
    {
         file = new File("Customers.txt");
       if(!(file.exists()))
       {
           try
           {
               file.createNewFile();
           }
           
           catch (IOException e)
           {
               
           }
       }
       old_customers = get_data();
    }
    
    public boolean save_changes(ArrayList<Customer> new_customers)
    {
        if(has_changed(new_customers))
        {
            
            try
            {
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                for(Customer member : new_customers)
                {
                    String line = member.give_string_data();
                    bw.write(line);
                    bw.newLine();
                }
                bw.close();
                old_customers = get_data();
                return true;
            }
            catch (IOException e)
            {
            }
            return false;
        }
        return true;
    }
    
    private boolean has_changed(ArrayList<Customer> new_customers)
    {
        int size;
        if((size = old_customers.size()) == new_customers.size())
        {
            for(int i = 0; i < size; i++)
            {
                if(!(old_customers.get(i).give_string_data().equals(new_customers.get(i).give_string_data())))
                {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
    public ArrayList<Customer> get_data()
    {
        ArrayList<Customer> customers_from_file = new ArrayList<>();
        String line;
        try
        {
            fr = new FileReader(file);
            try
            {
                br = new BufferedReader(fr);
                
                while ((line = br.readLine()) != null)
                {
                    if(line != "")
                    {
                        customers_from_file.add(retrieve_customer(line));
                    }
                }
                br.close();
            }
            catch (IOException e)
            {
            }
        }
        catch (FileNotFoundException ex)
        {
        }
        return customers_from_file;
    }
    
    private Customer retrieve_customer(String line)
    {
        String[] data = line.split("\t");
        return new Customer(data[0], data[1], data[2], data[3], data[4], data[5], data[8], make_array(data[6]), make_array(data[7]));
    }
    
    private ArrayList<String> make_array(String ids)
    {
        ArrayList<String> returner = new ArrayList<>();
        String[] data = ids.split(" ");
        for(String id : data)
        {
            returner.add(id);
        }
        return returner;
    }
    
        


}
